//发送模板消息
exports.templateMessagesSent = function(req, res) {

	//获取restful参数
	var bdoyParams = req.routeInfo.bodyParams;
	console.log('%c这是接收到的参数', 'color:#fac011')
	console.log(bdoyParams)

	//最终反回结果
	var resultData = {};

	//保存access_token
	var accessToken = "";

	//保存jsapi_ticket
	var jsapiTicket = "";

	//默认跳转用户中心链接
	var goMyHome = "http://" + config.publicConfig.SERVER_IP + "/#/user";
	//预约列表
	var goSubscribeList = "http://" + config.publicConfig.SERVER_IP + "/#/freeConsultingList";
	//我的代金券页面
	var goMyVoucher = "http://" + config.publicConfig.SERVER_IP + "/#/my_coupon";
	//机构主页
	var goMyOrganizationHome = "http://"+config.publicConfig.SERVER_IP+"/#/organization_manager_home";
	//机构订单
	var goMyOrganizationOrder = "http://" + config.publicConfig.SERVER_IP + "/#/my_organization_order";
	//机构退款申请订单
	var goMyOrganizationRefund = "http://" + config.publicConfig.SERVER_IP + "/#/organizational_refund_list";
	//	var goMyHome = "http://demo2.artmofang.com/#/user";

	//任务流创建
	async.waterfall([

		//获取accessToken
		function(cb) {

			R.SEND_HTTP(
				req, {
					url: "http://" + config.publicConfig.SERVER_IP + ":3400/weixin/getAccessToken",
					//					url: "http://demo2.artmofang.com:3400/weixin/getAccessToken",
					//					url: config.publicConfig.MIDDLE_API_SERVER + "weixin/getAccessToken",
					method: 'GET'
				},
				function(err, data) {

					if(err) {
						return RES.response(res, false, "公众号config配置签名获取失败");
					}

					access_token = data.access_token;

					//console.log(access_token)

					cb();

				}
			);

		},

		//获取模板内容
		function(cb) {
			if(bdoyParams.type == "dingDanChuLi") {
				//机构订单处理私信处理
				var postDatas = {
					"touser": bdoyParams.openid || "o6vy41XauQZqGlcRjGZqGrCj4gLk",
					"template_id": "VlYukbqBiaTZZtxir1Y1bAyEh8aCnqglKzxUZ6ppnNY", //好校长
//					"template_id": "5GB96fL-W3m3paBMrL774zfeDpa8NZG_z3BJM_LpzOs", //校营通
//					"template_id": "Xz2djXBxznC5unX721WDrGhsJOAwSWHTuVYA3R4Sqyc", //机构端
					"url": goMyOrganizationOrder, //详情链接 机构我的订单
					"data": {
						"first": {
							"value": "您好，您有一个新订单待处理！", //代金券标题
							"color": "#173177"
						},
						"keyword1": {
							"value": bdoyParams.userName, //学员姓名
							"color": "#173177"
						},
						"keyword2": {
							"value": bdoyParams.voucherName, //课程名称
							"color": "#173177"
						},
						"keyword3": {
							"value": bdoyParams.voucherMoney, //订单金额
							"color": "#173177"
						},
						"keyword4": {
							"value": "已支付", //订单状态
							"color": "#173177"
						},
						"keyword5": {
							"value": common.getNowFormatDate(), //订单时间
							"color": "#173177"
						},
						"remark": {
							"value": "请尽快与学员取得联系！\n点击此处登录后台处理订单！", //结束语
							"color": "#173177"
						}
					}
				}

			} else if(bdoyParams.type == "tuiKuanShenQingChuLi") {
				//机构退款申请处理私信处理
				var postDatas = {
					"touser": bdoyParams.openid || "o6vy41XauQZqGlcRjGZqGrCj4gLk",
					"template_id": "5GB96fL-W3m3paBMrL774zfeDpa8NZG_z3BJM_LpzOs", //校营通
//					"template_id": "Xz2djXBxznC5unX721WDrGhsJOAwSWHTuVYA3R4Sqyc", //机构端
					"url": goMyOrganizationRefund, //详情链接 机构我的退款列表
					"data": {
						"first": {
							"value": "您好，您有一个退款申请订单待处理！", //代金券标题
							"color": "#173177"
						},
						"keyword1": {
							"value": bdoyParams.userName, //学员姓名
							"color": "#173177"
						},
						"keyword2": {
							"value": bdoyParams.voucherName, //课程名称
							"color": "#173177"
						},
						"keyword3": {
							"value": bdoyParams.voucherMoney, //订单金额
							"color": "#173177"
						},
						"keyword4": {
							"value": "退款申请", //订单状态
							"color": "#173177"
						},
						"keyword5": {
							"value": common.getNowFormatDate(), //订单时间
							"color": "#173177"
						},
						"remark": {
							"value": "请尽快与学员取得联系！\n点击此处登录后台处理订单！", //结束语
							"color": "#173177"
						}
					}
				}

			} else if(bdoyParams.type == "yueKeShenQingChuLi") {
				var newRemark = bdoyParams.remark == '' ? "无" : bdoyParams.remark;
				//机构约课申请处理私信处理
				var postDatas = {
					"touser": bdoyParams.openid || "o6vy41XauQZqGlcRjGZqGrCj4gLk",
					"template_id": "5GB96fL-W3m3paBMrL774zfeDpa8NZG_z3BJM_LpzOs", //校营通
//					"template_id": "Xz2djXBxznC5unX721WDrGhsJOAwSWHTuVYA3R4Sqyc", //机构端
//					"url": goMyOrganizationHome, //详情链接 机构主页
					"data": {
						"first": {
							"value": "您好，您有一个约课申请订单待处理！", //代金券标题
							"color": "#173177"
						},
						"keyword1": {
							"value": bdoyParams.userName, //学员姓名
							"color": "#173177"
						},
						"keyword2": {
							"value": bdoyParams.voucherName, //课程名称
							"color": "#173177"
						},
						"keyword3": {
							"value": bdoyParams.voucherMoney, //订单金额
							"color": "#173177"
						},
						"keyword4": {
							"value": "约课申请", //订单状态
							"color": "#173177"
						},
						"keyword5": {
							"value": common.getNowFormatDate(), //订单时间
							"color": "#173177"
						},
						"remark": {
							"value": "请尽快与学员取得联系！\n联系电话："+bdoyParams.phone+"\n备注信息："+ newRemark, //结束语
							"color": "#173177"
						}
					}
				}

			} else if(bdoyParams.type == "baoMingChengGongTongZhi") {
				//报名成功通知-发送给机构管理员
				var postDatas = {
					"touser": bdoyParams.openid || "o6vy41XauQZqGlcRjGZqGrCj4gLk",
					"template_id": "wKXNePMK0QaeN98cuRUa0Kogy5jl0j-77h33wQyt0XE", //校营通
//					"template_id": "GRpw4vDNA4zqe1bxF0S-Ome0QAVFLJqdPx0g5-jn0UY", //机构端
					"url": bdoyParams.goUrls || goMyOrganizationRefund, //详情链接
					"data": {
						"first": {
							"value": "如何让暑期班学生快速续报秋季班？\n", //标题
							"color": "#ff0000"
						},
						"keyword1": {
							"value": "艺魔方“营销招生服务”正式上线，每月定时发送招生执行方案，配合七大线上工具，助力机构轻松完成当月招生任务！", //学员姓名
							"color": "#333333"
						},
						"keyword2": {
							"value": "7月招生执行方案已上传，我们帮您系统规划，提前做好秋季班续费的准备。\n", //课程名称
							"color": "#333333"
						},
						"remark": {
							"value": "我们将基于暑假班的续费转化，从“学生分类”、“如何增强家长体验”、“秋季班续费活动建议”等5个方面，帮您做好秋季班续费的准备。点击了解方案详情。", //结束语
							"color": "#ff0000"
						}
					}
				}

			}else if(bdoyParams.type == "zhuanFaYouLi") {
				//活动推送（转发有礼） - 服务完成通知
				var postDatas = {
					"touser": bdoyParams.openid || "o6vy41XauQZqGlcRjGZqGrCj4gLk",
					"template_id": "uS8KP6EbHT5ZPTxb8T_EDH6AxbtIJuwgqnNa_Q56vVo",//校营通
//					"template_id": "7qCO0l6AFEB27ZrnY8aVaBkeYBLHf__5jH4JctYQ5kY",//机构版
					"url": bdoyParams.goUrls, //详情链接
					"data": {
						"first": {
							"value": "恭喜您匹配【"+bdoyParams.o_name+"】转发有礼活动!", //标题
							"color": "#ff0000"
						},
						"keyword1": {
							"value": "转发有礼", //主题
							"color": "#333333"
						},
						"keyword2": {
							"value": bdoyParams.activity_time,  //时间
							"color": "#333333"
						},
						"remark": {
							"value": bdoyParams.content, //内容
							"color": "#333333"
						}
					}
				}

			}else if(bdoyParams.type == "huoDongTuiJian"){

				//活动推送（活动推荐） - 服务完成通知
				var postDatas = {
					"touser": bdoyParams.openid || "o6vy41XauQZqGlcRjGZqGrCj4gLk",
					"template_id": "uS8KP6EbHT5ZPTxb8T_EDH6AxbtIJuwgqnNa_Q56vVo",//校营通
//					"template_id": "7qCO0l6AFEB27ZrnY8aVaBkeYBLHf__5jH4JctYQ5kY",//机构版
					"url": bdoyParams.goUrls, //详情链接
					"data": {
						"first": {
							"value": "恭喜您匹配【"+bdoyParams.o_name+"】【"+bdoyParams.oa_name+"】", //标题
							"color": "#ff0000"
						},
						"keyword1": {
							"value": bdoyParams.oa_name, //主题
							"color": "#333333"
						},
						"keyword2": {
							"value": bdoyParams.activity_time,  //时间
							"color": "#333333"
						},
						"remark": {
							"value": "已经有 "+bdoyParams.user_count+" 人关注了本活动，点击围观参与吧", //内容
							"color": "#333333"
						}
					}
				}

			}else if(bdoyParams.type == "xinKeHuZiXun"){

				//新客户咨询
				var postDatas = {
					"touser": bdoyParams.openid || "oNriK0ZfubRWB63q63np_hobc1G0",
					"template_id": "atB1HR_8339DBK2zEQMb-T8Z6-_QdCpcYvABa6Vq_O4",//好校长
					"url": goSubscribeList, //详情链接
					"data": {
						"first": {
							"value": "您有来自“"+bdoyParams.dizhi+"”的客户咨询！", //来自地址
							"color": "#ff0000"
						},
						"keyword1": {
							"value": bdoyParams.lianxiren, //联系人名称
							"color": "#333333"
						},
						"keyword2": {
							"value": bdoyParams.dianhua,  //联系人电话
							"color": "#333333"
						},
						"keyword3": {
							"value": common.getNowFormatDate(),  //提交日期
							"color": "#333333"
						},
						"keyword4": {
							"value": "线上填单咨询",  //提交日期
							"color": "#333333"
						},
						"keyword5": {
							"value": "新客户咨询",  //提交日期
							"color": "#333333"
						},
						"remark": {
							"value": "请快速与该客户联系！", //内容
							"color": "#333333"
						}
					}
				}

			}
			

			var params = {
				"access_tokenData": access_token,
				"postData": JSON.stringify(postDatas)
			}

			//			console.log("我是传参--------------------")
			//			console.log(params)

			R.SEND_HTTP(
				req, {
					url: "http://" + config.publicConfig.SERVER_IP + "/weixinDemo/getWeChatMessage.php",
					//					url: "http://demo2.artmofang.com/weixinDemo/getWeChatMessage.php",
					method: 'POST',
					data: params
				},
				function(err, data) {

					console.log("我是返回--------------------")
					console.log(data)

					if(err) {
						return RES.response(res, false, "模板消息发送失败");
					}

					//					console.log("模板消息发送成功");

					cb();

				}
			);

		},

	], function(err, result) {

		//返回最终拼接结果
		RES.response(res, true, resultData);

	});

}